package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.TrainEvaluateNewLocalDataActivity;
import com.digitalchina.lxnsy.api.BusinessTraining;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TrainVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017/3/24 20:36
 * 修改备注：
 * ================================================
 */
public class TrainInfoListHolder extends BaseViewHolder<TrainVO> {


    private TextView tvTrainTitle;
    private TextView tvTrainName;
    private SimpleDraweeView simpleDraweeView;  // 专家头像
    private TextView tvTag; // 标记状态
    private TextView tvTrainTime; // 培训时间
    private TextView tvTrainAddress;
    private TextView tvNumber; // 培训人数


    private LinearLayout layoutLeft; // 左边按钮
    private ImageView ivLeft; // 左边图标
    private TextView tvLeft; // 左边文字

    private LinearLayout layoutRight; // 右边按钮
    private ImageView ivRight; // 右边图标
    private TextView tvRight; // 右边文字

    private String trainTime = "";

    private boolean isAttention;// 标记是否关注
    private Handler mhHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG.MSG_TO_ADD_ATTENTION_SUCCESS:// 点击关注
                    if (isAttention) {
                        showToast("已取消关注");
                        tvRight.setText("关注");
                        tvRight.setTextColor(getContext().getResources().getColor(R.color.app_color));
                        ivRight.setBackgroundResource(R.drawable.ic_attention);
                        layoutRight.setBackgroundResource(R.drawable.shape_app_line_rect);
                        isAttention = false;
                    } else {
                        showToast("关注成功");
                        tvRight.setText("已关注");
                        tvRight.setTextColor(getContext().getResources().getColor(R.color.colorFontGray));
                        ivRight.setBackgroundResource(R.drawable.ic_attention_ok);
                        layoutRight.setBackgroundResource(R.drawable.shape_app_line_rect_gray);
                        isAttention = true;
                    }

                    break;
                case MSG.MSG_TO_ADD_ATTENTION_FIELD:
                    showToast(msg.obj.toString());
                    break;
                case MSG.MSG_SIGN_SUCCESS:// 签到
                    String sign = FastJsonUtil.getString(msg.obj.toString(), "sign");
                    if (sign.equals("true"))
                        showToast("签到成功");
                    else
                        showToast("您已签到，请勿重复签到");
                    break;
                case MSG.MSG_SIGN_FIELD:
                    showToast(msg.obj.toString());
                    break;
            }
        }
    };

    /**
     * isSign: ,   是否签到
     * isEvaluate: , 是否评价
     * isAttention: , 是否关注
     * isExpire  是否过期  true已过期
     */

    public TrainInfoListHolder(ViewGroup parent) {
        super(parent, R.layout.item_train_info_list);
        tvTrainTitle = $(R.id.item_train_info_tv_title);
        tvTrainName = $(R.id.item_train_info_tv_name);
        simpleDraweeView = $(R.id.item_train_info_iv_head);
        tvTag = $(R.id.item_train_info_tv_tag);
        tvTrainTime = $(R.id.item_train_info_tv_time);
        tvTrainAddress = $(R.id.item_train_info_tv_address);
        tvNumber = $(R.id.item_train_info_tv_number);

        layoutLeft = $(R.id.item_train_info_layout_left);
        ivLeft = $(R.id.item_train_info_iv_left);
        tvLeft = $(R.id.item_train_info_tv_left);
        layoutRight = $(R.id.item_train_info_layout_right);
        ivRight = $(R.id.item_train_info_iv_right);
        tvRight = $(R.id.item_train_info_tv_right);

    }


    public void setData(final TrainVO trainVO) {
        super.setData(trainVO);

        tvTrainTitle.setText(trainVO.getTitle() != null ? trainVO.getTitle() : "");
        tvTrainName.setText(trainVO.getAgriTeacher() != null ? trainVO.getAgriTeacher().getName() : "匿名讲师");
        FrescoUtil.showImageSmall(trainVO.getAgriTeacher() != null ? trainVO.getAgriTeacher().getImg() : "", simpleDraweeView);
        tvNumber.setText(trainVO.getAttendanceNum() != null ? trainVO.getAttendanceNum() + "人" : "0人");
        tvTrainAddress.setText(trainVO.getFullAddress());

        // 培训时间
        for (int i = 0; i < trainVO.getTrainingTimes().size(); i++) {
            if (i == trainVO.getTrainingTimes().size())
                trainTime += trainVO.getTrainingTimes().get(i).getTrainDate() + " " + trainVO.getTrainingTimes().get(i).getStartTime() + "-" + trainVO.getTrainingTimes().get(i).getEndTime();
            else
                trainTime += trainVO.getTrainingTimes().get(i).getTrainDate() + " " + trainVO.getTrainingTimes().get(i).getStartTime() + "-" + trainVO.getTrainingTimes().get(i).getEndTime() + "\n";
        }

        tvTrainTime.setText(trainTime);
        trainTime = "";

        if (!trainVO.isIsSign()) { // 还没签到
            if (trainVO.isIsExpire()) { // 已经过期
                tvTag.setText("已过期");
                layoutLeft.setVisibility(View.GONE);
            } else {// 还没有过期
                if (!StringUtil.isStrEmpty(trainVO.getLocationAddress())) { // 定位成功
                    if (trainVO.getFullAddress().contains(trainVO.getLocationAddress())) { // 如果培训地址包含定位区域 显示签到按钮
                        tvTag.setText("待签到");
                        tvLeft.setText("签到");
                        layoutLeft.setVisibility(View.VISIBLE);
                        layoutLeft.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (!Utils.isLogin(getContext())) {
                                    Utils.notLoginGoto(getContext());
                                } else
                                    BusinessTraining.sign((Activity) getContext(), trainVO.getId(), mhHandler);
                            }
                        });
                    } else { // 不再定位区域内 显示距离过远
                        tvTag.setText("距离过远");
                        layoutLeft.setVisibility(View.GONE);
                    }
                } else {
                    tvTag.setText("距离过远");
                    layoutLeft.setVisibility(View.GONE);
                }

            }
        } else { // 已经签到
            layoutLeft.setVisibility(View.VISIBLE);
            if (trainVO.isIsEvaluate()) { // 已经评价
                tvTag.setText("已评价");
                tvLeft.setText("查看评价");
                tvLeft.setTextColor(getContext().getResources().getColor(R.color.app_color));
                ivLeft.setBackgroundResource(R.drawable.ic_evaluate);
                ivLeft.setVisibility(View.GONE);
                layoutLeft.setBackgroundResource(R.drawable.shape_app_line_rect);
                layoutLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!Utils.isLogin(getContext())) {
                            Utils.notLoginGoto(getContext());
                        } else
                            GotoUtil.gotoActivity((Activity) getContext(), TrainEvaluateNewLocalDataActivity.class, "evaluateDetail", trainVO.getId());
                    }
                });
            } else { // 没有评价
                tvTag.setText("待评价");
                tvLeft.setText("评价");
                tvLeft.setTextColor(getContext().getResources().getColor(R.color.app_color));
                ivLeft.setBackgroundResource(R.drawable.ic_evaluate);
                ivLeft.setVisibility(View.VISIBLE);
                layoutLeft.setBackgroundResource(R.drawable.shape_app_line_rect);
                layoutLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!Utils.isLogin(getContext())) {
                            Utils.notLoginGoto(getContext());
                        } else
                            GotoUtil.gotoForResultActivity((Activity) getContext(), TrainEvaluateNewLocalDataActivity.class, 132, "JPush_evaluate", trainVO.getId());
                    }
                });
            }

        }


        if (trainVO.isIsAttention()) { // 已关注
            tvRight.setText("已关注");
            isAttention = true;
            tvRight.setTextColor(getContext().getResources().getColor(R.color.colorFontGray));
            ivRight.setBackgroundResource(R.drawable.ic_attention_ok);
            layoutRight.setBackgroundResource(R.drawable.shape_app_line_rect_gray);
            layoutRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Utils.isLogin(getContext())) {
                        Utils.notLoginGoto(getContext());
                    } else
                        BusinessTraining.toAddAttention((Activity) getContext(), "", trainVO.getId(), mhHandler);
                }
            });
        } else { // 没有关注
            tvRight.setText("关注");
            isAttention = false;
            tvRight.setTextColor(getContext().getResources().getColor(R.color.app_color));
            ivRight.setBackgroundResource(R.drawable.ic_attention);
            layoutRight.setBackgroundResource(R.drawable.shape_app_line_rect);
            layoutRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Utils.isLogin(getContext())) {
                        Utils.notLoginGoto(getContext());
                    } else
                        BusinessTraining.toAddAttention((Activity) getContext(), "", trainVO.getId(), mhHandler);
                }
            });
        }

    }


    private void showToast(String toast) {
        ToastUtil.showToast(getContext(), toast);
    }
}
